Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
The brotli npm package is a compression library that implements the Brotli compression algorithm. It provides methods for compressing and decompressing data using Brotli in Node.js applications.
Compression
This feature allows you to compress data using the Brotli algorithm. The code sample demonstrates how to read a file, compress its contents, and then write the compressed data to a new file with a .br extension.
const brotli = require('brotli');
const fs = require('fs');
const input = fs.readFileSync('input.txt');
const compressed = brotli.compress(input);
fs.writeFileSync('input.txt.br', compressed);
Decompression
This feature allows you to decompress data that was previously compressed with the Brotli algorithm. The code sample shows how to read a compressed file, decompress its contents, and then write the decompressed data to a new file.
const brotli = require('brotli');
const fs = require('fs');
const compressed = fs.readFileSync('input.txt.br');
const decompressed = brotli.decompress(compressed);
fs.writeFileSync('output.txt', decompressed);
zlib is a compression library that offers compression and decompression functionalities similar to brotli but using different algorithms like Deflate and Gzip. It is built into Node.js, making it readily available without additional installation.
snappy is a package that provides bindings to Google's Snappy compression library. It is known for its high-speed compression and decompression, although it typically achieves lower compression ratios compared to Brotli.
lz4 is a package that implements LZ4 compression, which is known for its extremely fast compression and decompression speeds, but like snappy, it may not achieve compression ratios as high as Brotli.
Brotli.js is port of the Brotli compression algorithm (as used in the WOFF2 font format) to JavaScript. The decompressor is hand ported, and the compressor is ported with Emscripten. The original C++ source code can be found here.
Install using npm.
npm install brotli
If you want to use brotli in the browser, you should use Browserify to build it.
In node, or in browserify, you can load brotli in the standard way:
var brotli = require('brotli');
You can also require just the decompress
function or just the compress
function, which is useful for browserify builds.
For example, here's how you'd require just the decompress
function.
var decompress = require('brotli/decompress');
Decompresses the given buffer to produce the original input to the compressor.
The outSize
parameter is optional, and will be computed by the decompressor
if not provided. Inside a WOFF2 file, this can be computed from the WOFF2 directory.
// decode a buffer where the output size is known
brotli.decompress(compressedData, uncompressedLength);
// decode a buffer where the output size is not known
brotli.decompress(fs.readFileSync('compressed.bin'));
Compresses the given buffer. Pass optional parameters as the second argument.
// encode a buffer of binary data
brotli.compress(fs.readFileSync('myfile.bin'));
// encode some data with options (default options shown)
brotli.compress(fs.readFileSync('myfile.bin'), {
mode: 0, // 0 = generic, 1 = text, 2 = font (WOFF2)
quality: 11, // 0 - 11
lgwin: 22 // window size
});
MIT
FAQs
A port of the Brotli compression algorithm as used in WOFF2
The npm package brotli receives a total of 2,659,901 weekly downloads. As such, brotli popularity was classified as popular.
We found that brotli demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.